<!--  -->
<script setup>
import { reactive, ref } from 'vue'
import { NCheckbox } from 'naive-ui'

const emit = defineEmits(['sort-change'])

const sortParams = reactive({
  inventory: false,
  onlyDiscount: false,
  sortField: 'null',
  sortMethod: 'null'
})

const sortVal = reactive([
  {
    name: '默认排序',
    sortField: 'null'
  },
  {
    name: '最新商品',
    sortField: 'publishTime'
  },
  {
    name: '最高人气',
    sortField: 'orderNum'
  },
  {
    name: '评论最多',
    sortField: 'evaluateNum'
  }
])


const changeSort = (val) => {
  if (val === 'price') {
    sortParams.sortField = val
    // 处理排序
    if (sortParams.sortMethod === 'null') {
      sortParams.sortMethod = 'desc'
    } else {
      sortParams.sortMethod = sortParams.sortMethod === 'desc' ? 'asc' : 'desc'
    }
    emit('sort-change', sortParams)
    return
  }
  if (val === sortParams.sortField) return
  sortParams.sortField = val
  emit('sort-change', sortParams)


}

</script>

<template>
  <div class="sub-sort">
    <div class="sort">
      <a
        v-for="(item, key) in sortVal"
        :key="key"
        :class="{ active: sortParams.sortField == item.sortField }"
        @click="changeSort(item.sortField)"
        href="javascript:;"
      >{{ item.name }}</a>

      <a href="javascript:;" @click="changeSort('price')">
        价格排序
        <i
          class="arrow up"
          :class="{ active: sortParams.sortField == 'price' && sortParams.sortMethod == 'asc' }"
        />
        <i
          class="arrow down"
          :class="{ active: sortParams.sortField == 'price' && sortParams.sortMethod == 'desc' }"
        />
      </a>
    </div>
    <div class="check">
      <n-checkbox
        v-model:checked="sortParams.inventory"
        @update:checked="emit('sort-change', sortParams)"
        label="仅显示有货商品"
      ></n-checkbox>
      <n-checkbox
        v-model:checked="sortParams.onlyDiscount"
        @update:checked="emit('sort-change', sortParams)"
        label="仅显示特惠商品"
      ></n-checkbox>
    </div>
  </div>
</template>

<style lang='less' scoped>
.sub-sort {
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .sort {
    display: flex;
    a {
      height: 30px;
      line-height: 28px;
      border: 1px solid #e4e4e4;
      padding: 0 20px;
      margin-right: 20px;
      color: #999;
      border-radius: 2px;
      position: relative;
      transition: all 0.3s;
      &.active {
        background: @xtxColor;
        border-color: @xtxColor;
        color: #fff;
      }
      .arrow {
        position: absolute;
        border: 5px solid transparent;
        right: 8px;
        &.up {
          top: 3px;
          border-bottom-color: #bbb;
          &.active {
            border-bottom-color: @xtxColor;
          }
        }
        &.down {
          top: 15px;
          border-top-color: #bbb;
          &.active {
            border-top-color: @xtxColor;
          }
        }
      }
    }
  }
  .check {
    .xtx-checkbox {
      margin-left: 20px;
      color: #999;
    }
  }
}
</style>